package com.cmcc.aregister.ui.adapter;

import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.GlobalSetting;
import com.cmcc.aregister.cs.constant.HelpInc;
import com.cmcc.aregister.cs.model.Appointment;
import com.cmcc.aregister.cs.model.DataUtilEntity;
import com.cmcc.aregister.cs.util.Utils;

public class AutoAppointAdapter extends BaseAdapter
{
    private Context context;
    public List<Appointment> list;
    HelpInc helpInc;
    private static final String TAG = "AutoAppointAdapter";

    public AutoAppointAdapter(Context context, List<Appointment> list,
        HelpInc helpInc)
    {
        this.context = context;
        this.list = list;
        this.helpInc = helpInc;
    }

    public int getCount()
    {
        return list.size();
    }

    public Object getItem(int position)
    {
        return position;
    }

    public long getItemId(int position)
    {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
        if (convertView == null)
        {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                R.layout.a_auto_appoint_detail, null);
            holder.order_id_title = (TextView) convertView
                .findViewById(R.id.order_id_title);
            holder.order_id = (TextView) convertView
                .findViewById(R.id.order_id);
            holder.order_status = (TextView) convertView
                .findViewById(R.id.order_status);
            holder.order_time = (TextView) convertView
                .findViewById(R.id.order_time);
            holder.hospital_name = (TextView) convertView
                .findViewById(R.id.hospital_name);
            holder.order_info = (TextView) convertView
                .findViewById(R.id.order_info);
            holder.order_btn = (Button) convertView
                .findViewById(R.id.order_btn);
            holder.order_btn.setOnClickListener(mOnClickListener);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        Appointment app = list.get(position);
        Log.i(TAG, "位置:" + position + "  状态:" + app.yuyue_state);
        if (app.yuyue_state.equals("2"))
        {
            holder.order_id_title.setVisibility(View.VISIBLE);
            holder.order_id_title.setText("订单号：");
            holder.order_id.setVisibility(View.VISIBLE);
            holder.order_id.setText(app.order_no);
        }
        else if (app.yuyue_state.equals("3"))
        {
            holder.order_id_title.setVisibility(View.VISIBLE);
            holder.order_id_title.setText("失败描述：");
            holder.order_id.setVisibility(View.VISIBLE);
            holder.order_id.setText(app.service_desc);
        }
        else
        {
            holder.order_id_title.setVisibility(View.GONE);
            holder.order_id.setVisibility(View.GONE);
        }

        holder.order_status.setText(Utils
            .getAutoAppointTaskStatus(app.yuyue_state));
        holder.order_time.setText(app.begin_time);
        holder.hospital_name.setText(app.unit_name);
        holder.order_info.setText(app.dep_name + " " + app.doctor_name);
        if (app.yuyue_state.equals("0") || app.yuyue_state.equals("1"))
        {
            holder.order_btn.setText("取消任务");
            holder.order_btn.setVisibility(View.VISIBLE);
            DataUtilEntity pData = new DataUtilEntity();
            pData.msg = context.getString(R.string.pay_again);
            pData.position = position;
            holder.order_btn.setTag(pData);
        }
        else
        {
            holder.order_btn.setVisibility(View.GONE);
        }
        return convertView;
    }

    static class ViewHolder
    {
        TextView order_id_title;
        TextView order_id;
        TextView order_status;
        TextView order_time;
        TextView hospital_name, order_info;
        Button order_btn;
    }

    OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v)
        {
            DataUtilEntity mData = (DataUtilEntity) v.getTag();
            helpInc.notifyUI(mData.position);
        }
    };
}